package server.comm;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;

import messaage.Message;


public class ClientStub {

	private PrintWriter out;
	private Socket clientSocket;
	private String clientName;
	public boolean isConnected;
	private Encoder encoder_;
	private boolean isLoogedIn;

	public ClientStub(Socket clientSocket_, String clientName_,
			PrintWriter out_, Encoder encody) {
		this.clientSocket = clientSocket_;
		this.clientName = clientName_;
		this.out = out_;
		this.isConnected = false;
		this.encoder_ = encody;
		this.isLoogedIn = false;
	}


	public void send(Message m) {
	//	Encoder encode = new SimpleEncoder(charset);
	//	byte[] myMsg = encode.toBytes(msg);
	//	MESSAGE
	//	destination:/queue/a
	//	message-id: <message-identifier>

	//	hello queue a
	//	^@
		synchronized(out) {
			String outputStr = m.toString();
			//out.print(outputStr);
			//out.flush();
	        try {
	        byte[] buf = encoder_.toBytes(outputStr);

				clientSocket.getOutputStream().write(buf, 0, buf.length);

	        clientSocket.getOutputStream().flush();
			} catch (IOException e) {
			}
		}
	}


	/**
	 * @return the clientSocket
	 */
	public Socket getClientSocket() {
		return clientSocket;
	}


	/**
	 * @param clientSocket the clientSocket to set
	 */
	public void setClientSocket(Socket clientSocket) {
		this.clientSocket = clientSocket;
	}

	/**
	 * @return the clientName
	 */
	public String getClientName() {
		return clientName;
	}


	/**
	 * @param clientName the clientName to set
	 */
	public void setClientName(String clientName) {
		this.clientName = clientName;
	}


	/**
	 * @return the isConnected
	 */
	public boolean isConnected() {
		return isConnected && !clientSocket.isClosed();
	}


	/**
	 * @param isConnected the isConnected to set
	 */
	public void setConnected(boolean isConnected) {
		this.isConnected = isConnected;
	}


	public boolean isLoogedIn() {
		return true;//isLoogedIn;
	}


	public void setLoogedIn(boolean isLoogedIn) {
		this.isLoogedIn = isLoogedIn;
	}
}
